﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using EMANIFEST.DataAccess.Models;
using EMANIFEST.Dtos;

namespace EMANIFEST.DataAccess
{
    public partial class BusinessDataAccess
    {
        
        public string userName = string.Empty;

        #region GetModelById
        private DangerousGood GetDangerousGoodModelByDocumentId_Record(Guid documentId, Guid? userId, bool isTemp,Guid dangerousgood)
        {
            var ret = (from o in eManifestModel.DangerousGoods
                       where o.DocumentId == documentId && o.CreaterId == userId && o.IsTemp == isTemp && o.DangerousGoodId == dangerousgood
                       select o).FirstOrDefault() as DangerousGood;

            return ret;
        }

        public HouseBill GetHouseBillModelByDocumentId_Record(Guid documentId, Guid? userId, bool isTemp,Guid housebillId)
        {
            var ret = (from o in eManifestModel.HouseBills
                       where o.DocumentId == documentId && o.CreaterId == userId && o.IsTemp == isTemp && o.HouseBillId == housebillId
                       select o).FirstOrDefault() as HouseBill;

            return ret;
        }

        private GoodDeclaration GetGoodDeclarationModelByDocumentId_Record(Guid documentId, Guid? userId, bool isTemp,Guid gooddeclarationId)
        {
            var ret = (from o in eManifestModel.GoodDeclarations
                       where o.DocumentId == documentId && o.CreaterId == userId && o.IsTemp == isTemp && o.GoodDeclarationId == gooddeclarationId
                       select o).FirstOrDefault() as GoodDeclaration;

            return ret;
        }

        private ShipStoreDeclaration GetShipStoreDeclarationModelByDocumentId_Record(Guid documentId, Guid? userId, bool isTemp,Guid shipStoreDeclarationId)
        {
            var ret = (from o in eManifestModel.ShipStoreDeclarations
                       where o.DocumentId == documentId && o.CreaterId == userId && o.IsTemp == isTemp && o.ShipStoreDeclarationId == shipStoreDeclarationId
                       select o).FirstOrDefault() as ShipStoreDeclaration;

            return ret;
        }

        private GeneralDeclaration GetGeneralDeclarationModelByDocumentId_Record(Guid documentId, Guid? userId, bool isTemp,Guid generalDeclarationId)
        {
            var ret = (from o in eManifestModel.GeneralDeclarations
                       where o.DocumentId == documentId && o.CreaterId == userId && o.IsTemp == isTemp && o.GeneralDeclarationId == generalDeclarationId
                       select o).FirstOrDefault() as GeneralDeclaration;

            return ret;
        }

        #endregion
       
        #region Passenger
        private bool SavePassengerR_Sub(PassengerDto passengerDto)
        {
            var get = GetPassengerModelById(passengerDto.PassengerId);

            bool description = false;

            if (get != null)
            {
                // Update               
                get.FamilyName = passengerDto.FamilyName;
                get.GivenName = passengerDto.GivenName;
                get.StateId = passengerDto.StateId;
                get.BirthDate = passengerDto.BirthDate;
                get.BirthPlace = passengerDto.BirthPlace;
                get.PassportType = passengerDto.PassportType;
                get.PassportNumber = passengerDto.PassportNumber;
                get.PassportValidDate = passengerDto.PassportValidDate;
                get.ItemStatus = passengerDto.ItemStatus;
                get.IsTransit = passengerDto.IsTransit;
                get.PortIdOfEmbarkation = passengerDto.PortIdOfEmbarkation;
                get.PortNameOfEmbarkation = passengerDto.PortNameOfEmbarkation;
                get.PortOfDisembarkation = passengerDto.PortIdOfDisembarkation;
                get.PortNameOfDisembarkation = passengerDto.PortNameOfDisembarkation;

                //get.PortId = passengerDto.PortId;

                get.IsTemp = passengerDto.IsTemp;

                description = true;

            }
            else
            {
                // Insert
                get = ConvertPassengerDtoToModel(passengerDto);
               
                eManifestModel.Passengers.AddObject(get);
            }
            return description;
        }
        public ResponseDto SavePassengerRecord(PassengerDto passengerDto)
        {
            var ret = new ResponseDto { IsSuccess = true, ErroeMessage = string.Empty };

            if (passengerDto == null) return ret;

            try
            {
                string strdescription= string.Empty;
                bool isTemp = IsSubmitDocument(passengerDto.DocumentId);

                passengerDto.IsTemp = isTemp;
                // Update document status
                if (isTemp)
                {
                    SetDocumentStatusUpdate(passengerDto.DocumentId, enumDocumentUpdate.Passenger);
                }
                // Save 
                bool description = SavePassengerR_Sub(passengerDto);

                if(description == true)
                    strdescription  = "Cập nhập";
                else strdescription= " Thêm mới ";
                // Save documentHistory
                var get = GetDocumentModelById(passengerDto.DocumentId);
                SaveDocumentHistory(get, passengerDto.CreaterId.Value, userName, strdescription + " bản khai hành khách");
                // Save change
                eManifestModel.SaveChanges();

            }
            catch (Exception ex)
            {
                ret.IsSuccess = false;
                ret.ErroeMessage = ex.InnerException.Message;
            }

            return ret;
        }
        #endregion

        #region CrewEffectDeclaration
        private bool SaveCrewEffectDeclarationR_Sub(CrewEffectDeclarationDto crewEffectDeclarationDto)
        {
            bool description = false;
            var get = GetCrewEffectDeclarationModelById(crewEffectDeclarationDto.CrewEffectDeclarationId);

            if (get != null)
            {
                // Update
                get.FullName = crewEffectDeclarationDto.FullName;
                get.Rank = crewEffectDeclarationDto.Rank;
                get.DutyPackage = crewEffectDeclarationDto.DutyPackage;
                get.ModifyId = crewEffectDeclarationDto.ModifyId;
                get.CreaterId = crewEffectDeclarationDto.CreaterId;
                get.IsTemp = crewEffectDeclarationDto.IsTemp;
               
                description = true;
            }
            else
            {
                // Insert
                get = ConvertCrewEffectDeclarationDtoToModel(crewEffectDeclarationDto);
                eManifestModel.CrewEffectDeclarations.AddObject(get);
                
            }
            return description;
        }
        public ResponseDto SaveCrewEffectDeclarationRecord(CrewEffectDeclarationDto crewEffectDeclarationDto)
        {
            string strdescription = string.Empty;
            var ret = new ResponseDto { IsSuccess = true, ErroeMessage = string.Empty };

            if (crewEffectDeclarationDto == null) return ret;

            try
            {

                bool isTemp = IsSubmitDocument(crewEffectDeclarationDto.DocumentId);

                crewEffectDeclarationDto.IsTemp = isTemp;


                // Update document status
                if (isTemp)
                {
                    SetDocumentStatusUpdate(crewEffectDeclarationDto.DocumentId, enumDocumentUpdate.CrewEffectDeclaration);
                }

                // Save
                
                bool description  = SaveCrewEffectDeclarationR_Sub(crewEffectDeclarationDto);
                //SaveDocumentHistory
                if (description == true)
                    strdescription = "Cập nhập";
                else strdescription = " Thêm mới ";
                var get = GetDocumentModelById(crewEffectDeclarationDto.DocumentId);
                SaveDocumentHistory(get, crewEffectDeclarationDto.CreaterId.Value, userName, strdescription + " bản khai hành lý thuyền viên");
                // Save change
                eManifestModel.SaveChanges();
            }
            catch (Exception ex)
            {
                ret.IsSuccess = false;
                ret.ErroeMessage = ex.Message;
            }

            return ret;
        }
        #endregion

        #region Crew
        private bool SaveCrewR_Sub(CrewDto crewDto)
        {
            bool description = false;
            var get = GetCrewModelById(crewDto.CrewId);

            if (get != null)
            {
                // Update
                get.FamilyName = crewDto.FamilyName;
                get.GivenName = crewDto.GivenName;
                get.Rank = crewDto.Rank;
                get.StateId = crewDto.StateId;
                get.BirthDate = crewDto.BirthDate;
                get.BirthPlace = crewDto.BirthPlace;
                get.PassportType = crewDto.PassportType;
                get.PassportNumber = crewDto.PassportNumber;
                get.PassportValidDate = crewDto.PassportValidDate;
                get.ItemStatus = crewDto.ItemStatus;
               // get.CreaterId = crewDto.CreaterId;
                get.IsTemp = crewDto.IsTemp;
              
                description = true;
            }
            else
            {
                // Insert
                get = ConvertCrewDtoToModel(crewDto);
                eManifestModel.Crews.AddObject(get);
               
            }
            return description;
        }
        public ResponseDto SaveCrewRecord(CrewDto crewDto)
        {
            string strdescription = string.Empty;
            var ret = new ResponseDto { IsSuccess = true, ErroeMessage = string.Empty };

            if (crewDto == null) return ret;

            try
            {
                bool isTemp;
                isTemp = IsSubmitDocument(crewDto.DocumentId);

                crewDto.IsTemp = isTemp;

                // Update document status
                if (isTemp)
                {
                    SetDocumentStatusUpdate(crewDto.DocumentId, enumDocumentUpdate.Crew);
                }

                // Save
                bool description =  SaveCrewR_Sub(crewDto);
                //SaveDocumentHistory
                if (description == true)
                    strdescription = "Cập nhập";
                else strdescription = " Thêm mới ";
                var get = GetDocumentModelById(crewDto.DocumentId);
                SaveDocumentHistory(get, crewDto.CreaterId.Value, userName, strdescription + " danh sách thuyền viên");

                // Save change
                eManifestModel.SaveChanges();
            }
            catch (Exception ex)
            {
                ret.IsSuccess = false;
                ret.ErroeMessage = ex.Message;
            }

            return ret;
        }
        #endregion
        #region DangerousGood
       
        private bool SaveDangerousGoodR_Sub(DangerousGoodDto dangerousGoodDto)
        {
            bool description = false;
            // Check document submited
            dangerousGoodDto.IsTemp = IsSubmitDocument(dangerousGoodDto.DocumentId);

            // Update document status
            if (dangerousGoodDto.IsTemp)
            {
                SetDocumentStatusUpdate(dangerousGoodDto.DocumentId, enumDocumentUpdate.DangerousGood);
            }

            var get = GetDangerousGoodModelByDocumentId_Record(dangerousGoodDto.DocumentId,dangerousGoodDto.CreaterId,dangerousGoodDto.IsTemp, dangerousGoodDto.DangerousGoodId);
            
            if (get != null)
            {
                // Update
                // get.DangerousGoodId = dangerousGoodDto.DangerousGoodId;
                get.PortOfLoad = dangerousGoodDto.PortOfLoad;
                get.PortOfUnload = dangerousGoodDto.PortOfUnload;
                get.PortNameOfLoad = dangerousGoodDto.PortNameOfLoad;
                get.PortNameOfUnload = dangerousGoodDto.PortNameOfUnload;
                get.ContNumber = dangerousGoodDto.ContNumber;
                get.ContSealNumber = dangerousGoodDto.ContSealNumber;
                get.ItemStatus = dangerousGoodDto.ItemStatus;
               // get.CreaterId = dangerousGoodDto.CreaterId;
               // get.DocumentId = dangerousGoodDto.DocumentId;
                get.ModifyId = dangerousGoodDto.ModifyId;
                get.IsTemp = dangerousGoodDto.IsTemp;
               
                description = true;

            }
            else
            {
                // Insert
                get = ConvertDangerousGoodDtoToModel(dangerousGoodDto);
                eManifestModel.DangerousGoods.AddObject(get);
               

            }
            return description;

        }

        public ResponseDto SaveDangerousGoodRecord(DangerousGoodDto dangerousGoodDto)
        {
            string strdescription = string.Empty;
            var ret = new ResponseDto { IsSuccess = true, ErroeMessage = string.Empty };

            try
            {

                bool isTemp = IsSubmitDocument(dangerousGoodDto.DocumentId);

                dangerousGoodDto.IsTemp = isTemp;


                // Update document status
                if (isTemp)
                {
                    SetDocumentStatusUpdate(dangerousGoodDto.DocumentId, enumDocumentUpdate.DangerousGood);
                }

                // Save
                bool  description=    SaveDangerousGoodR_Sub(dangerousGoodDto);

                //SaveDocumentHistory
                if (description == true)
                    strdescription = "Cập nhập";
                else strdescription = " Thêm mới ";
                var get = GetDocumentModelById(dangerousGoodDto.DocumentId);
                SaveDocumentHistory(get, dangerousGoodDto.CreaterId.Value, userName, strdescription + " bản khai hàng hóa nguy hiểm");

                // Save change
                eManifestModel.SaveChanges();
            }
            catch (Exception ex)
            {
                ret.IsSuccess = false;
                ret.ErroeMessage = ex.InnerException.Message;
            }

            return ret;
        }

        public ResponseDto SaveDangerousGoodItemRecord(DangerousGoodItemDto dangerousGoodItemDto)
        {
            var ret = new ResponseDto { IsSuccess = true, ErroeMessage = string.Empty };

            try
            {
                var get = GetDangerousGoodItemModelById(dangerousGoodItemDto.DangerousGoodItemId);

                if (get != null)
                {
                    // Update 
                    get.RefNumber = dangerousGoodItemDto.RefNumber;
                    get.NumberContainer = dangerousGoodItemDto.NumberContainer;
                    get.NumberOfPackage = dangerousGoodItemDto.NumberOfPackage;
                    get.ProperShipping = dangerousGoodItemDto.ProperShipping;
                    get.GoodClass = dangerousGoodItemDto.GoodClass;
                    get.UNNumber = dangerousGoodItemDto.UNNumber;
                    get.PackingGroup = dangerousGoodItemDto.PackingGroup;
                    get.SubsidiaryRisk = dangerousGoodItemDto.SubsidiaryRisk;
                    get.FlashPoint = dangerousGoodItemDto.FlashPoint;
                    get.MarinePollutant = dangerousGoodItemDto.MarinePollutant;
                    get.GrossWeight = dangerousGoodItemDto.GrossWeight;
                    get.StowagePosition = dangerousGoodItemDto.StowagePosition;
                    get.ItemStatus = dangerousGoodItemDto.ItemStatus;
                }
                else
                {
                    // Insert
                    get = ConvertDangerousGoodItemDtoToModel(dangerousGoodItemDto);
                    eManifestModel.DangerousGoodItems.AddObject(get);
                }
                eManifestModel.SaveChanges();
            }
            catch (Exception ex)
            {
                ret.IsSuccess = false;
                ret.ErroeMessage = ex.Message;
            }

            return ret;
        }
        #endregion

        #region ShipStoreDeclaration
        private bool SaveShipStoreDeclarationR_Sub(ShipStoreDeclarationDto shipStoreDeclarationDto)
        {
            bool description = false;
            shipStoreDeclarationDto.IsTemp = IsSubmitDocument(shipStoreDeclarationDto.DocumentId);

            // Update document status
            if (shipStoreDeclarationDto.IsTemp)
            {
                SetDocumentStatusUpdate(shipStoreDeclarationDto.DocumentId, enumDocumentUpdate.ShipStoreDeclaration);
            }

            var get = GetShipStoreDeclarationModelByDocumentId_Record(shipStoreDeclarationDto.DocumentId,shipStoreDeclarationDto.CreaterId, shipStoreDeclarationDto.IsTemp, shipStoreDeclarationDto.ShipStoreDeclarationId);

            if (get != null)
            {
                // Update
                // get.ShipStoreDeclarationId = shipStoreDeclarationDto.ShipStoreDeclarationId;
                get.PeriodOfStay = shipStoreDeclarationDto.PeriodOfStay;
                get.PlaceOfStorage = shipStoreDeclarationDto.PlaceOfStorage;
                //get.CreaterId = shipStoreDeclarationDto.CreaterId;
                get.CreatedDate = shipStoreDeclarationDto.CreatedDate;
                get.ItemStatus = shipStoreDeclarationDto.ItemStatus;
                //get.DocumentId = shipStoreDeclarationDto.DocumentId;
                get.ModifyId = shipStoreDeclarationDto.ModifyId;
                // get.IsTemp = shipStoreDeclarationDto.IsTemp;
                description = true;

            }
            else
            {
                // Insert
                get = ConvertShipStoreDeclarationDtoToModel(shipStoreDeclarationDto);

                eManifestModel.ShipStoreDeclarations.AddObject(get);
               
            }
            return description;
        }
        public ResponseDto SaveShipStoreDeclarationRecord(ShipStoreDeclarationDto shipStoreDeclarationDto)
        {
            string strdescription = string.Empty;
                 
            var ret = new ResponseDto { IsSuccess = true, ErroeMessage = string.Empty };

            try
            {

                bool isTemp = IsSubmitDocument(shipStoreDeclarationDto.DocumentId);

                shipStoreDeclarationDto.IsTemp = isTemp;


                // Update document status
                if (isTemp)
                {
                    SetDocumentStatusUpdate(shipStoreDeclarationDto.DocumentId, enumDocumentUpdate.ShipStoreDeclaration);
                }
                // Save
             bool description = SaveShipStoreDeclarationR_Sub(shipStoreDeclarationDto);
                if (description == true)
                    strdescription = "Cập nhập";
                else strdescription = " Thêm mới ";
                //SaveDocumentHistory
                var get = GetDocumentModelById(shipStoreDeclarationDto.DocumentId);
                SaveDocumentHistory(get, shipStoreDeclarationDto.CreaterId.Value, userName, strdescription + " bản khai dự trữ tàu");

                // Save change
                eManifestModel.SaveChanges();

            }
            catch (Exception ex)
            {
                ret.IsSuccess = false;
                ret.ErroeMessage = ex.Message;
            }

            return ret;
        }

        public ResponseDto SaveShipStoreItemRecord(ShipStoreItemDto shipStoreItemDto)
        {
            var ret = new ResponseDto { IsSuccess = true, ErroeMessage = string.Empty };

            try
            {
                var get = GetShipStoreItemModelById(shipStoreItemDto.ShipStoreItemId);

                if (get != null)
                {
                    // Update 
                    get.Name = shipStoreItemDto.Name;
                    get.Quantity = shipStoreItemDto.Quantity;
                    get.ItemStatus = shipStoreItemDto.ItemStatus;
                    get.ShipStoreDeclarationId = shipStoreItemDto.ShipStoreDeclarationId;
                }
                else
                {
                    // Insert
                    get = ConvertShipStoreItemDtoToModel(shipStoreItemDto);
                    eManifestModel.ShipStoreItems.AddObject(get);
                }
                eManifestModel.SaveChanges();
            }
            catch (Exception ex)
            {
                ret.IsSuccess = false;
                ret.ErroeMessage = ex.Message;
            }

            return ret;
        }
        #endregion
        #region GeneralDeclaration

        private bool SaveGeneralDeclarationR_Sub(GeneralDeclarationDto generalDeclaration)
        {
            bool description = false;
            generalDeclaration.IsTemp = IsSubmitDocument(generalDeclaration.DocumentId);

            // Update document status
            if (generalDeclaration.IsTemp)
            {
                SetDocumentStatusUpdate(generalDeclaration.DocumentId, enumDocumentUpdate.GeneralDeclaration);
            }

            var get = GetGeneralDeclarationModelByDocumentId_Record(generalDeclaration.DocumentId,generalDeclaration.CreaterId,generalDeclaration.IsTemp, generalDeclaration.GeneralDeclarationId);

            if (get != null)
            {
                // Update
                // get.GeneralDeclarationId = generalDeclaration.GeneralDeclarationId;
                get.DeparturePortId = generalDeclaration.DeparturePortId;
                get.LastArrivalPortId = generalDeclaration.LastArrivalPortId;
                get.DateOfArrival = generalDeclaration.DateOfArrival;
                get.ContactDetails = generalDeclaration.ContactDetails;
                get.PreviousPorts = generalDeclaration.PreviousPorts;
                get.SubsequentPorts = generalDeclaration.SubsequentPorts;
                get.ParticularsOfVoyage = generalDeclaration.ParticularsOfVoyage;
                get.PositionInPort = generalDeclaration.PositionInPort;
                get.DescriptionOfCargo = generalDeclaration.DescriptionOfCargo;
                get.NumberOfCrews = generalDeclaration.NumberOfCrews;
                get.NumberOfPassengers = generalDeclaration.NumberOfPassengers;
                get.Remarks = generalDeclaration.Remarks;
                get.Requirements = generalDeclaration.Requirements;
                get.ModifiedDate = generalDeclaration.ModifiedDate;
                // get.CreaterId = generalDeclaration.CreaterId;
                get.ItemStatus = generalDeclaration.ItemStatus;
                // get.DocumentId = generalDeclaration.DocumentId;
                //get.ModifyId = generalDeclaration.ModifyId;
                get.IsTemp = generalDeclaration.IsTemp;
                description = true;
            }
            else
            {
                // Insert
                get = ConvertGeneralDeclarationDtoToModel(generalDeclaration);
                eManifestModel.GeneralDeclarations.AddObject(get);
                
            }
            return description;
        }

        public ResponseDto SaveGeneralDeclarationRecord(GeneralDeclarationDto generalDeclaration)
        {
            string strdescription = string.Empty;
            var ret = new ResponseDto { IsSuccess = true, ErroeMessage = string.Empty };

            try
            {

                bool isTemp = IsSubmitDocument(generalDeclaration.DocumentId);

                generalDeclaration.IsTemp = isTemp;


                // Update document status
                if (isTemp)
                {
                    SetDocumentStatusUpdate(generalDeclaration.DocumentId, enumDocumentUpdate.GeneralDeclaration);
                }
                // Save
            bool description = SaveGeneralDeclarationR_Sub(generalDeclaration);
                //SaveDocumentHistory
                if (description == true)
                    strdescription = "Cập nhập";
                else strdescription = " Thêm mới ";
                var get = GetDocumentModelById(generalDeclaration.DocumentId);
                SaveDocumentHistory(get, generalDeclaration.CreaterId, userName, strdescription + " bản khai chung");
                // Save change
                eManifestModel.SaveChanges();

            }
            catch (Exception ex)
            {
                ret.IsSuccess = false;
                ret.ErroeMessage = ex.Message;
            }

            return ret;
        }
        #endregion
       
        #region HouseBill
        private bool SaveHouseBillR_Sub(HouseBillDto houseBillDto)
        {
            bool description = false;
            houseBillDto.IsTemp = IsSubmitDocument(houseBillDto.DocumentId);

            // Update document status
            if (houseBillDto.IsTemp)
            {
                SetDocumentStatusUpdate(houseBillDto.DocumentId, enumDocumentUpdate.HouseBill);
            }

            var get = GetHouseBillModelByDocumentId_Record(houseBillDto.DocumentId,houseBillDto.CreaterId,houseBillDto.IsTemp, houseBillDto.HouseBillId); 

            if (get != null)
            {
                // Update

                get.Consigner = houseBillDto.Consigner;
                get.Consignee = houseBillDto.Consignee;
                get.PortOfTranship = houseBillDto.PortIdOfTranship;
                get.PortOfDestination = houseBillDto.PortIdOfDestination;
                get.PortOfLoad = houseBillDto.PortIdOfLoad;
                get.PortOfUnload = houseBillDto.PortIdOfUnload;
                get.BillNumber = houseBillDto.BillNumber;
                get.DateOfBill = houseBillDto.DateOfBill;
                get.DepartureDate = houseBillDto.DepartureDate;
                // get.CreaterId = houseBillDto.CreaterId;
                get.CreatedDate = houseBillDto.CreatedDate;
                get.ItemStatus = houseBillDto.ItemStatus;
                get.IsTemp = houseBillDto.IsTemp;

                description = true;
                // Save list GoodItems

            }
            else
            {
                // Insert
                get = ConvertHouseBillDtoToModel(houseBillDto);
                // Add and Update
                eManifestModel.HouseBills.AddObject(get);
                
            }
            return description;
        }
        public ResponseDto SaveHouseBillItemRecord(HouseBillItemDto houseBillItem)
        {
            var ret = new ResponseDto { IsSuccess = true, ErroeMessage = string.Empty };

            try
            {
                var get = GetHouseBillItemModelById(houseBillItem.HouseBillItemId);

                if (get != null)
                {
                    // Update                                       
                    get.Description = houseBillItem.Description;
                    get.GrossWeight = houseBillItem.GrossWeight;
                    get.Demension = houseBillItem.Demension;
                    get.ContNumber = houseBillItem.ContNumber;
                    get.ContSealNumber = houseBillItem.ContSealNumber;
                    get.ItemStatus = houseBillItem.ItemStatus;
                    get.ItemCodeId = houseBillItem.ItemCodeId;
                }
                else
                {
                    // Insert
                    get = ConvertHouseBillItemDtoToModel(houseBillItem);
                    eManifestModel.HouseBillItems.AddObject(get);
                }

                // Save change
                eManifestModel.SaveChanges();
            }
            catch (Exception ex)
            {
                ret.IsSuccess = false;
                ret.ErroeMessage = ex.Message;
            }

            return ret;
        }
       

        public ResponseDto SaveHouseBillRecord(HouseBillDto houseBillDto)
        {
            string strdescription = string.Empty;
            var ret = new ResponseDto { IsSuccess = true, ErroeMessage = string.Empty };

            try
            {

                bool isTemp = IsSubmitDocument(houseBillDto.DocumentId);

                houseBillDto.IsTemp = isTemp;


                // Update document status
                if (isTemp)
                {
                    SetDocumentStatusUpdate(houseBillDto.DocumentId, enumDocumentUpdate.HouseBill);
                }
                // Save 
            bool description = SaveHouseBillR_Sub(houseBillDto);
                //SaveDocumentHistory
                if (description == true)
                    strdescription = "Cập nhập";
                else strdescription = " Thêm mới ";
                var get = GetDocumentModelById(houseBillDto.DocumentId);
                SaveDocumentHistory(get, houseBillDto.CreaterId, userName, strdescription + " vận đơn gom hàng");
                // Save change
                eManifestModel.SaveChanges();

            }
            catch (Exception ex)
            {
                ret.IsSuccess = false;
                ret.ErroeMessage = ex.Message;
            }

            return ret;
        }
        #endregion

        #region GoodDeclaration
        private bool SaveGoodDeclarationR_Sub(GoodDeclarationDto goodDeclarationDto)
        {
            bool description = false;
            goodDeclarationDto.IsTemp = IsSubmitDocument(goodDeclarationDto.DocumentId);

            // Update document status
            if (goodDeclarationDto.IsTemp)
            {
                SetDocumentStatusUpdate(goodDeclarationDto.DocumentId, enumDocumentUpdate.GoodDeclaration);
            }

            var get = GetGoodDeclarationModelByDocumentId_Record(goodDeclarationDto.DocumentId,goodDeclarationDto.CreaterId, goodDeclarationDto.IsTemp, goodDeclarationDto.GoodDeclarationId);

            if (get != null)
            {
                // Update
                // get.GoodDeclarationId = goodDeclarationDto.GoodDeclarationId;
                get.Decription = goodDeclarationDto.Decription;
                get.NumberOfPackage = goodDeclarationDto.NumberOfPackage;
                get.KindOfPackages = goodDeclarationDto.KindOfPackages;
                //get.CreaterId = goodDeclarationDto.CreaterId;
                get.CreatedDate = goodDeclarationDto.CreatedDate;
                get.ItemStatus = goodDeclarationDto.ItemStatus;
                //get.DocumentId = goodDeclarationDto.DocumentId;
                get.ModifyId = goodDeclarationDto.ModifyId;
                get.IsTemp = goodDeclarationDto.IsTemp;
                description = true;
            }
            else
            {
                // Insert
                get = ConvertGoodDeclarationDtoToModel(goodDeclarationDto);

                // Add and Update

                eManifestModel.GoodDeclarations.AddObject(get);
                
            }
            return description;
        }


        public ResponseDto SaveGoodDeclarationRecord(GoodDeclarationDto goodDeclarationDto)
        {
            string strdescription = string.Empty;
            var ret = new ResponseDto { IsSuccess = true, ErroeMessage = string.Empty };

            try
            {
                bool isTemp = IsSubmitDocument(goodDeclarationDto.DocumentId);

                goodDeclarationDto.IsTemp = isTemp;


                // Update document status
                if (isTemp)
                {
                    SetDocumentStatusUpdate(goodDeclarationDto.DocumentId, enumDocumentUpdate.GoodDeclaration);
                }
                // Save 
              bool description = SaveGoodDeclarationR_Sub(goodDeclarationDto);
                //SaveDocumentHistory
                if (description == true)
                    strdescription = "Cập nhập";
                else strdescription = " Thêm mới ";
                var get = GetDocumentModelById(goodDeclarationDto.DocumentId);
                SaveDocumentHistory(get, goodDeclarationDto.CreaterId.Value, userName, strdescription + " bản khai hàng hóa");
                // Save change
                eManifestModel.SaveChanges();

            }
            catch (Exception ex)
            {
                ret.IsSuccess = false;
                ret.ErroeMessage = ex.Message;
            }

            return ret;
        }

        public ResponseDto SaveGoodItemRecord(GoodItemDto goodItemDto)
        {
            var ret = new ResponseDto { IsSuccess = true, ErroeMessage = string.Empty };

            try
            {
                var get = GetGoodItemModelById(goodItemDto.GoodItemId);

                if (get != null)
                {
                    // Update
                    get.MasterBillNo = goodItemDto.MasterBillNo;
                    get.Consigner = goodItemDto.Consigner;
                    get.Consignee = goodItemDto.Consignee;
                    get.NotificatedParty = goodItemDto.NotificatedParty;
                    get.ContNumber = goodItemDto.ContNumber;
                    get.ContSealNumber = goodItemDto.ContSealNumber;
                    get.GoodDescription = goodItemDto.GoodDescription;
                    get.NetWeight = goodItemDto.NetWeight;
                    get.GrossWeight = goodItemDto.GrossWeight;
                    get.Demension = goodItemDto.Demension;
                    get.RefNumber = goodItemDto.RefNumber;
                    get.Ajustment = goodItemDto.Ajustment;
                    get.ItemStatus = goodItemDto.ItemStatus;
                    get.GoodDeclarationId = goodItemDto.GoodDeclarationId;
                    get.ItemCodeId = goodItemDto.ItemCodeId;
                }
                else
                {
                    // Insert
                    get = ConvertGoodItemDtoToModel(goodItemDto);
                    eManifestModel.GoodItems.AddObject(get);
                }
                eManifestModel.SaveChanges();
            }
            catch (Exception ex)
            {
                ret.IsSuccess = false;
                ret.ErroeMessage = ex.Message;
            }

            return ret;
        }
        #endregion

        #region DocumentHistory
         public ResponseDto SaveDocumentHistoryUI(Guid documentId,string userName, string description)
        {
            var error = new ResponseDto { IsSuccess = true, ErroeMessage = string.Empty };
            try
            {

                var get = GetDocumentById(documentId);
                if (get != null)
                {
                    var ret = new DocumentHistory
                        {
                            DocumentHistoryId = Guid.NewGuid(),
                            DocumentId = get.DocumentId,
                            ModifiedDate = DateTime.Now, 
                            StatusId = get.StatusId.Value,
                            CreaterName = userName,
                            Description = description,
                        };

                    eManifestModel.DocumentHistories.AddObject(ret);
                    eManifestModel.SaveChanges();
                }
            }
             catch (Exception ex)
            {
                error.IsSuccess = false;
                error.ErroeMessage = ex.Message;
            }

            return error;
  
         }
       
        #endregion
      
       

    }
}
